<?php 
class Survey
{
	public $Id;
	public $SiteId;
	public $Title;
	public $Password;
	public $Description;
	public $DescriptionPrint;
	public $Guide;
	public $Active;
	public $Section;
	public $Sections;
	public $Demogs;
	public $DemogValues;
	public $DemogsPrint;
	public $SubLoaded;
	public $Response;
	public $Responses;
	public $ResponsesCount;
	public $ResponsesDemogCount;
	public $DataChanged;
	
	public function getId() { return $this->Id; }
	public function getSiteId() { return $this->SiteId; }
	public function getTitle() { return $this->Title; }
	public function getPassword() { return $this->Password; }
	public function getDescription() { return $this->Description; }
	public function getGuide() { return $this->Guide; }
	public function getActive() { return $this->Active; }
	public function getSection() { return $this->Section; }
	public function getSections() { return $this->Sections; }
	public function getDemogs() { return $this->Demogs; }
	public function getDemogValues() { return $this->DemogValues; }
	public function getSubLoaded() { return $this->SubLoaded; }
	public function getResponse() { return $this->Response; }
	public function getResponses() { return $this->Responses; }
	public function getResponsesCount() 
	{ 
		if(isset($this->ResponsesCount))
		{ return $this->ResponsesCount; }
		else
		{ 
			self::loadResponsesCount(); 
			return $this->ResponsesCount;
		}
	}
	public function getResponsesDemogCount($value) 
	{ 
		if($value > 0)
		{
			if(isset($this->ResponsesDemogCount[$value]))
			{ return $this->ResponsesDemogCount[$value]; }
			else
			{ 
				self::loadResponsesDemogCount($value); 
				return $this->ResponsesDemogCount[$value];
			}
		}
		else
		{ self::getResponsesCount(); }
	}
	public function getDataChanged() { return $this->DataChanged; }
	
	public function setId($val) { $this->Id=$val; }
	public function setSiteId($val) { $this->SiteId=$val; }
	public function setTitle($val) { $this->Title=$val; }
	public function setPassword($val) { $this->Password=$val; }
	public function setDescription($val) { $this->Description=$val; }
	public function setDescriptionPrint($val) { $this->DescriptionPrint=$val; }
	public function setGuide($val) { $this->Guide=$val; }
	public function setActive($val) { $this->Active=$val; }
	public function setSection($val) { $this->Section=$val; }
	public function setSections($val) { $this->Sections=$val; }
	public function setDemogs($val) { $this->Demogs=$val; }
	public function setDemogValues($val) { $this->DemogValues=$val; }
	public function setDemogsPrint($val) { $this->DemogsPrint=$val; }
	public function setSubLoaded($val) { $this->SubLoaded=$val; }
	public function setResponse($val) { $this->Response=$val; }
	public function setResponses($val) { $this->Responses=$val; }
	public function setResponsesCount($val) { $this->ResponsesCount=$val; }
	public function setDataChanged($val) { $this->DataChanged=$val; }
	
	public function Survey() 
	{ 
		self::setDataChanged(false);
		self::setSubLoaded(false); 
	}
	
	public function loadSections($guide = false)
	{
		if(($this->Id > 0) && ($this->SubLoaded == false))
		{
			unset($this->Sections);
			if($guide)
			{ $select = ' `id`, `section_type`, `section_order`, `active`, `title`, `guide` '; }
			else
			{ $select = ' `id`, `section_type`, `section_order`, `active`, `title` '; }
			
			$sql = 'SELECT '.$select.' FROM '.DB_PERFIX.'survey_sections WHERE survey_id='.$this->Id.' ORDER BY `section_order`, `id`;';
			$result = mysql_query($sql);
			if($result)
			{
				while($row = mysql_fetch_array($result))
				{
					$this->Sections[$row['id']] = new SurveySection();
					$this->Sections[$row['id']]->setId($row['id']);
					$this->Sections[$row['id']]->setSurveyId($this->Id);
					$this->Sections[$row['id']]->setType($row['section_type']);
					$this->Sections[$row['id']]->setOrder($row['section_order']);
					$this->Sections[$row['id']]->setActive($row['active']);
					$this->Sections[$row['id']]->setTitle($row['title']);
				}
			}
		}
		self::setSubLoaded(true);
	}
	
	public function loadDemogs()
	{
		if($this->Id > 0)
		{
			unset($this->Demogs);
			$sql = 'SELECT `id`, `title`, `active` FROM '.DB_PERFIX.'survey_demography WHERE survey_id='.$this->Id.' ORDER BY `title` ASC;';
			$result = mysql_query($sql);
			if($result)
			{
				while($row = mysql_fetch_array($result))
				{
					$this->Demogs[$row['id']] = new SurveyDemography();
					$this->Demogs[$row['id']]->setId($row['id']);
					$this->Demogs[$row['id']]->setSurveyId($this->Id);
					$this->Demogs[$row['id']]->setActive($row['active']);
					$this->Demogs[$row['id']]->setTitle($row['title']);
				}
			}
			self::loadDemogValues();
		}
	}
	
	public function loadDemogValues()
	{
		if($this->Id > 0)
		{
			if($this->Demogs)
			{
				$sql = 'SELECT `id`, `demography_id`, `value`, `active` FROM '.DB_PERFIX.'survey_demography_values WHERE survey_id='.$this->Id.' ORDER BY `demography_id`, `id` ASC;';
				$result = mysql_query($sql);
				if($result)
				{
					while($row = mysql_fetch_array($result))
					{
						$this->DemogValues[$row['id']] = new DemographyValue();
						$this->DemogValues[$row['id']]->setId($row['id']);
						$this->DemogValues[$row['id']]->setSurveyId($this->Id);
						$this->DemogValues[$row['id']]->setDemogId($row['demography_id']);
						$this->DemogValues[$row['id']]->setValue($row['value']);
						$this->DemogValues[$row['id']]->setActive($row['active']);
							
						if($this->Demogs[$row['demography_id']])
						{ $this->Demogs[$row['demography_id']]->Values[$row['id']] = $this->DemogValues[$row['id']]; }
					}
				}
			}
		}
	}
	
	public function loadSection($id)
	{
		if(($this->Id > 0) && (($this->SubLoaded == true) && ($this->Sections)))
		{
			unset($this->Section);
			$this->Section = new SurveySection();
			$this->Section = $this->Sections[$id];
			
			$sql = 'SELECT guide FROM '.DB_PERFIX.'survey_sections WHERE id='.$id.';';
			$result = mysql_query($sql);
			if($result)
			{
				while($row = mysql_fetch_array($result))
				{ $this->Section->setGuide($row['guide']); }
			}
			$this->Section->loadQuestions();
		}
	}
	
	public function update()
	{
		if(($this->Id > 0) && ($this->DataChanged == true))
		{
			$sql  = 'UPDATE '.DB_PERFIX.'survey SET ';
			$sql .= '`title`=\''.mysql_real_escape_string($this->Title).'\', ';
			if(isset($this->Description))
			{ $sql .= '`description`=\''.$this->Description.'\', '; }
			if(isset($this->Guide))
			{ $sql .= '`guide`=\''.$this->Guide.'\', '; }
			$sql .= '`password`=\''.mysql_real_escape_string($this->Password).'\' ';
			$sql .= 'WHERE `id`='.$this->Id.';';
			$result = mysql_query($sql);
		}
	}
	
	public function loadResponses()
	{
		unset($this->Responses);
		if($this->Id > 0)
		{
			$sql = 'SELECT id, subject, valid, complete FROM '.DB_PERFIX.'survey_response WHERE survey_id='.$this->Id.';';
			$result = mysql_query($sql);
			if($result)
			{
				while($row = mysql_fetch_array($result))
				{ 
					$this->Responses[$row['id']] = new SurveyResponse();
					$this->Responses[$row['id']]->setId($row['id']);
					$this->Responses[$row['id']]->setSubject($row['subject']);
					$this->Responses[$row['id']]->setValid($row['valid']);
					$this->Responses[$row['id']]->setComplete($row['complete']);
				}
			}
		}
	}
	
	public function loadResponse($id)
	{
		if(isset($this->Responses[$id]))
		{ 
			unset($this->Response);
			$this->Response = new SurveyResponse();
			$this->Response = $this->Responses[$id];
			$this->Response->LoadValues();
		}
	}
	
	public function clearResponses()
	{
		self::loadResponses();
		if($this->Responses)
		{
			$sql = 'DELETE FROM '.DB_PERFIX.'survey_response_demog_vals WHERE survey_id='.$this->Id.';';
			$result = mysql_query($sql);
			foreach($this->Responses as $response)
			{
				$sql = 'DELETE FROM '.DB_PERFIX.'survey_answer_values_scale WHERE response_id='.$response->getId().';';
				$result = mysql_query($sql);
			}
			$sql = 'DELETE FROM '.DB_PERFIX.'survey_response WHERE survey_id='.$this->Id.';';
			$result = mysql_query($sql);
			
			unset($this->Responses);
			$this->ResponsesCount = 0;
		}
	}
	
	
	public function loadResponsesCount()
	{
		$this->ResponsesCount = 0;
		if($this->Id > 0)
		{
			$sql = 'SELECT COUNT(id) AS responses FROM '.DB_PERFIX.'survey_response WHERE survey_id='.$this->Id.';';
			$result = mysql_query($sql);
			if($result)
			{
				while($row = mysql_fetch_array($result))
				{ $this->ResponsesCount = $row['responses']; }
			}
		}
	}

	public function loadResponsesDemogCount($value=0)
	{
		if($value > 0)
		{
			$this->ResponsesDemogCount[$value] = 0;
			if($this->Id > 0)
			{
				$sql = 'SELECT COUNT(id) AS responses FROM '.DB_PERFIX.'survey_response WHERE survey_id='.$this->Id.' AND id IN (SELECT response_id FROM '.DB_PERFIX.'survey_response_demog_vals WHERE value='.$value.');';
				$result = mysql_query($sql);
				if($result)
				{
					while($row = mysql_fetch_array($result))
					{ $this->ResponsesDemogCount[$value] = $row['responses']; }
				}
			}
		}
	}
	
	public function getResponsesDemogsCount($values)
	{
		$count = 0;
		if(($this->Id > 0) && (count($values) > 0))
		{
			$sql = 'SELECT COUNT(id) AS responses FROM '.DB_PERFIX.'survey_response WHERE survey_id='.$this->Id.' ';
			
			foreach($values as $value)
			{ $sql .= ' AND id IN (SELECT response_id FROM '.DB_PERFIX.'survey_response_demog_vals WHERE value='.$value.')'; }
			$sql .= ';';
			
			
			
			$result = mysql_query($sql);
			if($result)
			{
				while($row = mysql_fetch_array($result))
				{ $count = $row['responses']; }
			}
		}
		return $count;
	}
}
?>